package com.darker.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.darker.reggie.common.R;
import com.darker.reggie.entity.User;
import com.darker.reggie.service.UserService;
import com.darker.reggie.utils.SMSUtils;
import com.darker.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    RedisTemplate redisTemplate;
    //发送手机短信验证码
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user){
        log.info("userid=>{}",user.getPhone());
        String code[]=new String[1];
        //获取手机号码
        String phone = user.getPhone();
        //生成随机的验证码
        if(StringUtils.isNotEmpty(user.getPhone())){
             code[0] = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("生成的验证码为{}",code);
//           SMSUtils.sendMessage(user.getPhone(),code);
            //将生成的验证码保存到Session
//            session.setAttribute("code",code[0]);
            //验证码5分钟失效
            redisTemplate.opsForValue().set(phone,code[0],5 , TimeUnit.MINUTES);

            return R.success("发送成功");
        }
        return R.error("发送失败");
        //调用阿里云提供的短信服务API完成发送短信
    }
    @PostMapping("/login")
    public R<User> login(@RequestBody Map map, HttpSession session){
        log.info("map=>{}",map);
        String phone = map.get("phone").toString();
        String code = map.get("code").toString();
//        String code1 = (String) session.getAttribute("code");
        //从缓冲中取验证码
        String code1= (String) redisTemplate.opsForValue().get(phone);
        if(code.equals(code1)&&StringUtils.isNotEmpty(code1)){
            //判断当前手机号对应的用户是否是新用户 如果是新用户 自动完成注册
            LambdaQueryWrapper<User> queryWrappe=new LambdaQueryWrapper<>();
            queryWrappe.eq(phone!=null,User::getPhone,phone);
            User user = userService.getOne(queryWrappe);

            if(user==null){
                user=new User();
                user.setPhone(phone);
                userService.save(user);
            }
            //用户登录成功
            session.setAttribute("user",user.getId());
            //从缓存中删除验证码
            redisTemplate.delete(phone);
            return R.success(user);
        }
        return R.error("验证码错误");
    }
    @PostMapping("loginout")
    public R<String> loginout(HttpSession session){
        session.removeAttribute("user");
        return R.success("退出成功");
    }

}
